﻿using LD.Tool;
using System;
using System.Data;
using System.Linq;
using YTE.Control;
using 右下角弹窗.DiagShow;

namespace LD.Host
{
    public partial class FrmFlowEdit : FrmOKCancel
    {
        private string m_Name = string.Empty;
        public FrmFlowEdit()
        {
            InitializeComponent();
            this.Shown += FrmFlowEdit_Shown;
            this.btn_ok.Click += Btn_ok_Click;
            this.cob_flowType.SelectedIndex = 0;
        }
        public FrmFlowEdit(string name) : this()
        {
            var flowList = Appbus.PartNo.Config.Flow.Where(p => p.FlowType == 1).ToList();
            foreach (var item in flowList)
            {
                this.cob_flow.Items.Add(item.Name);
            }

            m_Name = name;
            var flow = Appbus.PartNo.Config.Flow.Where(p => p.Name == m_Name).FirstOrDefault();
            if (flow != null)
            {
                this.txt_flowName.Text = flow.Name;
                this.num_sort.Value = flow.Sort;
                this.cob_flowType.SelectedIndex = flow.FlowType;
                this.chk_errorRunFlow.Checked = flow.IsRunErrorFlow;
                this.cob_flow.Text = flow.ErrorFlow;
            }
        }

        private void FrmFlowEdit_Shown(object sender, EventArgs e)
        {
            
        }

        private void Btn_ok_Click(object sender, EventArgs e)
        {
            Dialog dialog = new Dialog();
            var _flow = Appbus.PartNo.Config.Flow.Where(p => p.Name == m_Name).FirstOrDefault();
            if (_flow != null)
            {
                if (string.IsNullOrEmpty(m_Name))
                {
                    dialog.Warning("重复存在");
                    return;
                }
                else
                {
                    //update
                    _flow.Name = this.txt_flowName.Text;
                    _flow.Sort = (int)this.num_sort.Value;
                    _flow.FlowType = this.cob_flowType.SelectedIndex;
                    _flow.IsRunErrorFlow = this.chk_errorRunFlow.Checked;
                    _flow.ErrorFlow = this.cob_flow.Text;
                    //更新子项
                    foreach (var item in _flow.Tools)
                    {
                        item.FlowName = _flow.Name;
                    }
                    return;
                }
            }
            var flow = new TB_Flow()
            {
                Name = this.txt_flowName.Text,
                Sort = (int)this.num_sort.Value,
                FlowType = this.cob_flowType.SelectedIndex,
                IsRunErrorFlow = this.chk_errorRunFlow.Checked,
                ErrorFlow = this.cob_flow.Text
            };
            Flow = flow;
            this.Close();
        }
        public TB_Flow Flow { get; set; }

    }
}
